package fy.hr.staffRedeploy;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;

import fy.hr.bean.DepBean;
import fy.hr.bean.OptionBean;


public final class StaffRedeploymentManager {

	/**
	 * 获取公司列表Option
	 * 
	 * @return
	 */
	public static void initStaffRedeployment(ModelAndView modelAndView) {
		StaffRedeployFormBean bean = StaffRedeploymentDao
				.getStaffRedeploymentBean();
		List<OptionBean> list = bean.getCompanyList();
		StringBuilder sb = new StringBuilder();
		if (list != null) {

			for (int i = 0, length = list.size(); i < length; i++) {
				sb.append(" <option value =").append(list.get(i).getId())
						.append(">").append(list.get(i).getName())
						.append("</option>");
			}
		}

		modelAndView.addObject("companyOption", sb.toString());
	}

	/**
	 * 获取公司列表Option
	 * 
	 * @return
	 */
	public static String queryDepInfo(String operate, String id) {
		// List<CompanyBean> list = StaffRedeploymentDao.getCompanyList();
		// StringBuilder sb = new StringBuilder();
		// if (list != null) {
		// for (int i = 0, length = list.size(); i < length; i++) {
		// sb.append(" <option value =").append(list.get(i).getId())
		// .append(">").append(list.get(i).getName())
		// .append("</option>");
		// }
		// }
		String result = null;
		if ("1".equals(operate)) {
			// 查询一级部门
			List<OptionBean> list = StaffRedeploymentDao.querAllDep(id);

			if (list != null) {
				JSONArray ja = new JSONArray();
				for (int i = 0, length = list.size(); i < length; i++) {
					ja.put(list.get(i).toString());
				}
				result = ja.toString();
			}
		} else if ("2".equals(operate)) {
			// 子部门和当前部门岗位
			DepBean bean = StaffRedeploymentDao.querChildrenDepAndPost(id);
			if (bean != null) {
				result = bean.toString();
			}
		} else if ("3".equals(operate)) {
			// 根据选择的部门查询其主管
			DepBean bean = StaffRedeploymentDao.querChildrenDepAndPost(id);
			if (bean != null) {
				result = bean.toString();
			}
		} else if ("4".equals(operate)) {
			// 查询调动人员信息
			StaffRedeployFormBean bean = StaffRedeploymentDao
					.queryMemberInfo(id);
			if (bean != null) {
				JSONObject jo = new JSONObject();
				try {
					jo.put("memberCode", id);
					jo.put("name", bean.getName());
					jo.put("joinTime", bean.getJoinTime());
					jo.put("education", bean.getEducation());
					jo.put("nowPost", bean.getNowPost());
					jo.put("nowLevel", bean.getNowLevel());
					jo.put("dep2", bean.getDep2());
					jo.put("dep1", bean.getDep1());
					jo.put("corp", bean.getCorp());
				} catch (Exception e) {
					e.printStackTrace();
				}
				result = jo.toString();
			}
		}

		return result;
	}
}
